package com.zk.erp.controller;


import com.zk.erp.entity.Functions;
import com.zk.erp.entity.JsonResult;
import com.zk.erp.service.IFunctionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zk
 * @since 2021-04-12
 */
@RestController
public class FunctionsController {
    @Autowired
    private IFunctionsService service;
    //按username查询所有的权限
    @ResponseBody
    @RequestMapping(value = "/functions-username",method = RequestMethod.GET)
    public JsonResult findList(){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

            if (principal!=null||principal instanceof User){
                User user = (User) principal;
                List<Functions> functions = service.findFunctionsByUsername(user.getUsername());
                return new JsonResult(1,functions);

        }
        return new JsonResult(0,null);
    }
    //查询所有的权限
    @RequestMapping(value = "/functions",method = RequestMethod.GET)
    public JsonResult findAll(){
        List<Functions> functions = service.findAll();
        return new JsonResult(1,functions);
    }

    @RequestMapping(value = "/functions/role/{roleId}",method = RequestMethod.GET)
    public JsonResult selectFunctionIdsByRoleId(@PathVariable("roleId")String roleId){
        List<String> ids = service.selectFunctionIdsByRoleId(roleId);
        return new JsonResult(1,ids);
    }
}
